package com.yfux.cling_player.service.controller;

import static org.fourthline.cling.model.types.ErrorCode.INVALID_ARGS;

import android.content.Context;
import android.content.Intent;

import com.yfux.cling_player.parcelables.CastAction;
import com.yfux.cling_player.ui.VideoPlayerActivity;
import com.yfux.cling_player.util.LogUtil;

import org.fourthline.cling.support.avtransport.AVTransportException;
import org.fourthline.cling.support.model.DeviceCapabilities;
import org.fourthline.cling.support.model.MediaInfo;
import org.fourthline.cling.support.model.PositionInfo;
import org.fourthline.cling.support.model.TransportAction;
import org.fourthline.cling.support.model.TransportInfo;
import org.fourthline.cling.support.model.TransportSettings;

import java.net.URI;

public abstract class AvTransportControl extends RenderControl {
    final String actionSetAvTransport = "com.dlna.action.SetAvTransport";
    final String keyExtraCastAction = "extra.castAction";
    /**
     * 被子类初始化
     */
    protected LogUtil logger;
    protected Context applicationContext;
    protected DeviceCapabilities deviceCapabilities;
    protected MediaInfo mediaInfo = new MediaInfo();
    protected PositionInfo positionInfo = new PositionInfo();
    protected TransportInfo transportInfo = new TransportInfo();
    protected TransportSettings transportSettings;

    public void setPlayMode(String newPlayMode) {
        logger.i("setPlayMode: newPlayMode=" + newPlayMode);
    }

    public void play(String speed) {
        logger.i("play: speed=" + speed);
    }

    public void pause() {
        logger.i("pause");
    }

    public void seek(String unit, String target) {
        logger.i("seek: unit=" + unit + ", target=" + target);
    }

    public void previous() {
        logger.i("previous");
    }

    public void next() {
        logger.i("next");
    }

    public void stop() {
        logger.i("stop");
    }

    private void startCastActivity(CastAction castAction) {
        Intent intent = new Intent(applicationContext, VideoPlayerActivity.class);
        intent.putExtra(keyExtraCastAction, castAction);
        // start from service content,should add 'FLAG_ACTIVITY_NEW_TASK' flag.
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        applicationContext.startActivity(intent);
    }

    public void setAVTransportURI(String currentURI, String currentURIMetaData) throws AVTransportException {
        logger.i("setAVTransportURI: currentURI=" + currentURI);
        if (currentURIMetaData != null)
            logger.i("setAVTransportURI: currentURIMetaData=" + currentURIMetaData);
        try {
            new URI(currentURI);
        } catch (Exception ex) {
            throw new AVTransportException(INVALID_ARGS, "CurrentURI can not be null or malformed");
        }
        CastAction castAction = new CastAction();
        castAction.setCurrentURI(currentURI);
        castAction.setCurrentURIMetaData(currentURIMetaData);
        startCastActivity(castAction);
    }

    public void setNextAVTransportURI(String nextURI, String nextURIMetaData) {
        logger.i("setNextAVTransportURI: nextURI=" + nextURI);
        if (nextURIMetaData != null)
            logger.i("setNextAVTransportURI: nextURIMetaData=" + nextURIMetaData);
        CastAction castAction = new CastAction();
        castAction.setNextURI(nextURI);
        castAction.setNextURIMetaData(nextURIMetaData);
        startCastActivity(castAction);
    }

    public MediaInfo getMediaInfo() {
        return mediaInfo;
    }

    public PositionInfo getPositionInfo() {
        return positionInfo;
    }

    public TransportInfo getTransportInfo() {
        return transportInfo;
    }

    public TransportAction[] getCurrentTransportActions() {
        return null;
    }

    public DeviceCapabilities getDeviceCapabilities() {
        return deviceCapabilities;
    }

    public TransportSettings getTransportSettings() {
        return transportSettings;
    }
}